package com.njlime.sys.repository;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.entities.Role;
import com.njlime.sys.entities.Unit;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2017-7-18.
 */
@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role> {

    /**
     * 根据用户ID获取列表
     *
     * @param userId
     * @return
     */
    public List<Role> listByUserId(Integer userId) {
        List<Role> roleList = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_role t ");
        sb.append("join t_user_role ur on t.id = ur.role_id ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("where t.is_del = 0 and u.is_del = 0 and ur.user_id = ? ");
        arrList.add(userId);
        //dataScopeFilter(userId, sb, arrList, "u");
        roleList = findList(sb.toString(), arrList);
        return roleList;
    }

    /**
     * 获取全部列表
     *
     * @return
     */
    public List<Role> findAllList() {
        List<Role> roleList = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_user_role t where is_del = 0");
        roleList = findList(sb.toString(), arrList);
        return roleList;
    }

    /**
     * 分页获取列表
     *
     * @param pageBean
     * @param role
     * @return
     */
    public PageBean list(PageBean pageBean, Role role) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,u.unit_name unit_name from t_role t ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        if(!StringUtil.isEmpty(role.getUser_name())) {
            sb.append("left join t_user_role ur on t.id = ur.role_id ");
            sb.append("left join t_user user on ur.user_id = user.id ");
        }
        sb.append("where t.is_del = 0 and u.is_del = 0 ");
        List<Role> roles = UserUtils.getRoleList();
        sb.append("and (");
        for(int i = 0; i < roles.size(); i++){
            sb.append("u.parent_ids like concat(?,'%') ");
            if(i < roles.size() - 1){
                sb.append("or ");
            }
            arrList.add(roles.get(i).getUnit().getParent_ids() + roles.get(i).getUnit().getId() + ",");
        }
        sb.append(") ");
        sb.append("and (u.id = ? or u.parent_ids like concat(?,'%')) ");
        arrList.add(UserUtils.getUser().getUnit().getId());
        arrList.add(UserUtils.getUser().getUnit().getParent_ids() + UserUtils.getUser().getUnit().getId() + ",");
        if(!StringUtil.isEmpty(role.getUser_name())) {
            sb.append("and user.login_name like concat('%',?,'%')");
            arrList.add(role.getUser_name());
        }
        if (!StringUtil.isEmpty(role.getName())) {
            sb.append("and t.name like concat('%',?,'%') ");
            arrList.add(role.getName());
        }
        if (role.getUnit_id() != null) {
            sb.append("and t.unit_id = ? ");
            arrList.add(role.getUnit_id());
        }
        if (!StringUtil.isEmpty(role.getName_en())) {
            sb.append("and t.name_en like concat('%',?,'%') ");
            arrList.add(role.getName_en());
        }
        if (role.getData_scope() != null) {
            sb.append("and t.data_scope = ? ");
            arrList.add(role.getData_scope());
        }
        if (role.getStatus() != null) {
            sb.append("and t.status = ? ");
            arrList.add(role.getStatus());
        }
        //dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "u", "t");
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        pageBean.setTotalPage(pageBean.getTotalPage());
        if (num > 0) {
            sb.append(" order by t.id desc limit ?,? ");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 根据名称获取角色
     *
     * @param name
     * @return
     */
    public Role getByName(String name) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_role where is_del = 0 and name = ? ");
        arrList.add(name);
        List<Role> re = findList(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 根据英文名称获取角色
     *
     * @param en_name
     * @return
     */
    public Role getByEnName(String en_name) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_role where is_del = 0 and name_en = ? ");
        arrList.add(en_name);
        List<Role> re = findList(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 角色更新
     *
     * @param role
     * @return
     */
    public int updateRole(Role role) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_role set update_time = ?,updater = ? ");
        arrList.add(role.getUpdate_time());
        arrList.add(role.getUpdater());
        if (!StringUtil.isEmpty(role.getName())) {
            sb.append(",name = ? ");
            arrList.add(role.getName());
        }
        if (!StringUtil.isEmpty(role.getName_en())) {
            sb.append(",name_en = ? ");
            arrList.add(role.getName_en());
        }
        if (role.getData_scope() != null) {
            sb.append(",data_scope = ? ");
            arrList.add(role.getData_scope());
        }
        if (role.getUnit_id() != null) {
            sb.append(",unit_id = ? ");
            arrList.add(role.getUnit_id());
        }
        if (role.getStatus() != null) {
            sb.append(",status = ? ");
            arrList.add(role.getStatus());
        }
        sb.append(" where id = ?");
        arrList.add(role.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    public int deleteRole(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_role set is_del = 1 where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色id删除用户角色
     *
     * @param id
     */
    public void deleteUserRole(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from t_user_role where role_id = ? ");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 添加角色用户
     *
     * @param role_id
     * @param user_id
     * @return
     */
    public int insertUserRole(Integer role_id, Integer user_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("insert into t_user_role (user_id,role_id) values (?,?)");
        arrList.add(user_id);
        arrList.add(role_id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色id删除角色权限
     *
     * @param id
     */
    public void deleteRoleMenu(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from t_role_menu where role_id = ? ");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 添加角色菜单
     *
     * @param role_id
     * @param menu_id
     * @return
     */
    public int insertRoleMenu(Integer role_id, Integer menu_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("insert into t_role_menu (role_id,menu_id) values (?,?)");
        arrList.add(role_id);
        arrList.add(menu_id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色id删除部门
     *
     * @param id
     */
    public void deleteRoleUnit(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from t_role_unit where role_id = ? ");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 添加角色部门
     *
     * @param role_id
     * @param unit_id
     * @return
     */
    public int insertRoleUnit(Integer role_id, Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("insert into t_role_unit (role_id,unit_id) values (?,?)");
        arrList.add(role_id);
        arrList.add(unit_id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色id删除用户角色
     *
     * @param user_id
     */
    public void delUserRoleByUser(Integer user_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from t_user_role where user_id = ? ");
        arrList.add(user_id);
        updateJdbc(sb.toString(), arrList);
    }

}
